package com.example.flowermall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.flowermall.entity.*;
import com.example.flowermall.service.ICategoryService;
import com.example.flowermall.service.ICollectionService;
import com.example.flowermall.service.IProductImgService;
import com.example.flowermall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoman
 * @since 2023-06-13
 */
@RestController
@RequestMapping("/goods")
public class ProductController {
    @Autowired
    IProductService productService;
    @Autowired
    IProductImgService productImgService;
    @Autowired
    ICollectionService collectionService;
    @Autowired
    ICategoryService categoryService;
    @GetMapping("/list")
    public Result getGoodsList(@RequestParam(value = "pageNo",required = false)Long pageNo,
                               @RequestParam(value = "pageSize",required = false)Long pageSize,
                               @RequestParam(value = "productName",required = false)String productName){
        Page<Product> page=new Page<>(pageNo,pageSize);
        Map<String,Object> data=new HashMap<>();
        Result result=Result.success();
        LambdaQueryWrapper<Product> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(productName),Product::getProductName,productName);
        Page<Product> page1 = productService.page(page, queryWrapper);
        List<Product> records = page1.getRecords();
        for (Product record : records) {
            record.setProfit(record.getPrice().doubleValue()-record.getCostPrice());
            List<ProductImg> listByProductId = productImgService.getListByProductId(record.getProductId());
            ProductImg byProductId = productImgService.getByProductId(record.getProductId());
            Category categoryById = categoryService.getCategoryById(record.getCategoryId());
            record.setCategoryName(categoryById.getCategoryName());
            if (byProductId != null) {
                record.setProductUrl(byProductId.getImgUrl());
            }
            if (listByProductId != null) {
                record.setProductImgList(listByProductId);
            }
        }
        data.put("total",page.getTotal());
        data.put("rows",records);
        result.put("data",data);
        return result;
    }
    @GetMapping("/lists")
    public Result selectGoods(@RequestParam(value = "productName",required = false)String productName){
        List<Product> product = productService.selectGoods(productName);
        Result result=Result.success();
        result.put("data",product);
        return result;
    }
    @PostMapping
    public Result insertGoods(@RequestBody Product product){
        return productService.insertGoods(product);
    }
    @GetMapping("/{id}")
    public Result getGoodsById(@PathVariable("id")Integer id,@RequestParam(value = "userId",required = false)Integer userId){
        Product goods = productService.getGoodsById(id);
        String categoryName = productService.selectCategory(id);
        List<ProductImg> listByProductId = productImgService.getListByProductId(id);
        Result result=Result.success();
        goods.setCategoryName(categoryName);
        if(userId!=null){
            List<Collect> collectList = collectionService.getByUserId(userId);
            if(collectList!=null){
                for (Collect collect : collectList) {
                    if (Objects.equals(collect.getProductId(), id)) {
                        result.put("isCollection",1);
                    }
                }
            }

        }

        result.put("data",goods);
        result.put("img",listByProductId);
        return result;
    }


    @PutMapping
    public Result updateGoods(@RequestBody Product product){
        productService.updateGoods(product);
        return Result.success("修改成功");
    }
    @DeleteMapping("/{id}")
    public Result deleteGoods(@PathVariable("id")Integer id){
        productService.deleteGoods(id);
        return Result.success("删除成功");
    }

    @GetMapping("/category/{categoryId}")
    public Result selectByCategoryId(@RequestParam("categoryId")Integer id){
        List<Product> productList = productService.selectGoodsByCategoryId(id);
        Result result=Result.success();
        result.put("goodsList",productList);
        return result;
    }

    @GetMapping("/tag/{tagId}")
    public Result selectByTagId(@RequestParam("tagId")Integer id){
        List<Product> productList = productService.selectByTagId(id);
        return Result.success(productList);
    }
    @GetMapping("/all")
    public Result getAll(){
        List<Product> list = productService.getAllCategory();
        Result result=Result.success();
        result.put("data",list);
        return result;
    }

}
